package com.sxj.corejava.code13_JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test07_多表保存 {

    public static void main(String[] args) throws DataAccessException {
//        Dept dept = new Dept("研发部","研发产品");
//        Integer deptId = insertReturnPrimaryKey(dept);
//
//        Emp e1 = new Emp("admin",8000.0,deptId);
//        Emp e2 = new Emp("jack",9000.0,deptId);
//        Emp e3 = new Emp("lucy",7000.0,deptId);
//        insertEmp(e1);
//        insertEmp(e2);
//        insertEmp(e3);


        Dept dept = new Dept("测试部2", "为公司赚钱的");
        Integer deptId = insertReturnPrimaryKey(dept);
        // 如果属性中使用的不是外键字段,而是对象的话,需要将返回的主键的值赋值给对应对象
        dept.setId(deptId);

        Emp e1 = new Emp("张三", 6000.0, dept);
        Emp e2 = new Emp("李四", 9000.0, dept);
        Emp e3 = new Emp("王五", 12000.0, dept);
        insertEmp(e1);
        insertEmp(e2);
        insertEmp(e3);
    }

    public static Integer insertReturnPrimaryKey(Dept dept) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_dept ")
                    .append("   (name,remark)")
                    .append(" values ")
                    .append("   (?,?)")
                    .toString();
            ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1, dept.getName());
            ps.setString(2, dept.getRemark());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            while (rs.next()) {
                id = rs.getInt(1);
            }
//            int i = 1 / 0;
        } catch (DataAccessException e) {
            throw e;
        } catch (SQLException e) {
            throw new DataAccessException(e);
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }
        return id;
    }

    public static void insertEmp(Emp emp) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp ")
                    .append("   (name,salary,dept_id)")
                    .append(" values ")
                    .append("   (?,?,?)")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setDouble(2, emp.getSalary());
            // 外键字段直接使用字段名
            // ps.setInt(3,emp.getDeptId());
            // 外键字段通过对象属性来表示
            ps.setInt(3, emp.getDept().getId());
            ps.executeUpdate();
        } catch (DataAccessException e) {
            throw e;
        } catch (SQLException e) {
            throw new DataAccessException(e);
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }
    }

}
